import router from '@/router'
import axios from 'axios'
import { Message } from 'element-ui'

const service = axios.create({
  baseURL: 'https://api-hmzs.itheima.net/v1',
  timeout: 5000 // request timeout
})

// 请求拦截器
service.interceptors.request.use(
  config => {
    config.headers.Authorization = 'Bearer ' + localStorage.getItem('token')
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    // console.dir(error)
    //判断token是否过期
    if (error.response.status === 401) {
      router.push('/login')
      Message.error('登录已过期，请重新登录')
    }

    return Promise.reject(error)
  }
)

export default service
// service({
//   url:'/park/login',
//   method:'POST',
//   data:{
//     username:'admin',
//     password:'admin123'
//   }
// }).then((res)=>{
//   console.log(res.data)
// })